﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class LightingRenderTexture : MonoBehaviour
{
    
    public Material screenMat;

    //Cams

    public Camera BGCam,innerWallCam,propCam,lightCam,playerCam,relayCam,edgeCam;

    
    private RenderTexture lightRT,edgeRT,BGRT,propRT,playerRT,relayRT,innerWallRT;

    private float camSize{get=>Camera.main.orthographicSize;}
    // Start is called before the first frame update
    private void Start()
    {
        CreateAndSetRT(ref BGRT,BGCam);
        CreateAndSetRT(ref innerWallRT,innerWallCam);
        CreateAndSetRT(ref propRT,propCam);
        CreateAndSetRT(ref lightRT,lightCam);
        CreateAndSetRT(ref playerRT,playerCam);
        CreateAndSetRT(ref relayRT,relayCam);
        CreateAndSetRT(ref edgeRT,edgeCam);
        //screenMat set
        screenMat.SetTexture("_BGCamRT",BGRT);
        screenMat.SetTexture("_InnerWallCamRT",innerWallRT);
        screenMat.SetTexture("_LightCamRT",lightRT);
        screenMat.SetTexture("_PropCamRT",propRT);
        screenMat.SetTexture("_RelayCamRT",relayRT);
        screenMat.SetTexture("_PlayerCamRT",playerRT);
        screenMat.SetTexture("_EdgeCamRT",edgeRT);
    }

    private void Update() {
        float currentSize = camSize;
        BGCam.orthographicSize = currentSize;
        innerWallCam.orthographicSize = currentSize;
        propCam.orthographicSize = currentSize;
        lightCam.orthographicSize = currentSize;
        playerCam.orthographicSize = currentSize;
        relayCam.orthographicSize = currentSize;
        edgeCam.orthographicSize = currentSize;

    }

    private void OnRenderImage(RenderTexture src, RenderTexture dst)
    {
        if (screenMat != null)
        {
            Graphics.Blit(src, dst, screenMat);
        }
    }

    private void CreateAndSetRT(ref RenderTexture rt,Camera cam){
        rt = new RenderTexture(Screen.width, Screen.height, 24);               
        cam.targetTexture = rt;        
    }
}